<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\mall\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\mall\model\Merch as MerchModel;
use app\user\model\User as UserModel;
use think\facade\Db;

/**
 * 回收站控制器
 * @package app\mall\admin
 */
class Recycle extends Admin
{
    /**
     * 商户列表
     * @param string $model 内容模型id
     * @author PaulXun <paulxun@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 查询
        $map   = $this->getMap();
        $map[] = ['trash', '=', 1];
        // 排序
        $order = $this->getOrder('update_time desc');
        // 数据列表
        $data_list = MerchModel::where($map)->order($order)->paginate(config('paginate'));
        
        // 自定义按钮
        $btnRestore = [
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'icon'  => 'fa fa-fw fa-reply',
            'title' => '还原',
            'href'  => url('restore', ['ids' => '__id__'])
        ];
        $btnRestoreAll = [
            'class' => 'btn btn-success ajax-post confirm',
            'icon'  => 'fa fa-fw fa-reply-all',
            'title' => '批量还原',
            'href'  => url('restore')
        ];
        // 返回按钮
        $btnRe = [
            'class' => 'btn btn-default',
            'icon'  => 'fa fa-fw fa-reply',
            'title' => '返回',
        	'href'  => url('mall/merch/index')
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setSearch(['merch_name' => '商户名称', 'short_name' => '商户简称']) // 设置搜索框
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['short_name', '商户简称'],
				['merch_name', '商户全称'],
                ['update_time', '更新时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('enable', $btnRestoreAll) // 批量添加顶部按钮
            ->addTopButton('delete', ['title' => '批量删除', 'href' => url('delete'), 'data-tips' => '删除后不可回复！']) // 批量添加顶部按钮
			->addTopButton('custom', $btnRe) // 回收站
            ->addRightButton('custom', $btnRestore) // 添加右侧按钮
            ->addRightButton('delete', ['href' => url('delete', ['ids' => '__id__']), 'data-tips' => '删除后不可回复！']) // 添加右侧按钮
            ->addOrder('id,title,view,username,update_time')
            ->setRowList($data_list) // 设置表格数据
			->setTableName('mall_merch') // 设置数据表名
            ->fetch(); // 渲染模板
    }

    /**
     * 还原商户
     * @param null $ids 商户id
     * @author PaulXun <paulxun@qq.com>
     */
    public function restore($ids = null)
    {
        if ($ids === null) $this->error('请选择要操作的数据');
		
		// 还原商户
        if (false === MerchModel::where('id', 'in', $ids)->update(['trash'=> 0])) {
            $this->error('还原失败');
        }
		
		if (is_array($ids)) {
			foreach ($ids as $id){
				cache('merch_info_'.$id, null);
			}
		} else {
			cache('merch_info_'.$ids, null);
		}
		
		$merch_id    = is_array($ids) ? '' : $ids;
		$titles = MerchModel::where('id', 'in', $ids)->column('short_name');
        // 记录日志
        action_log('merch_restore', 'mall_merch', $merch_id, UID, implode('、', $titles));
        $this->success('还原成功');
    }

    /**
     * 彻底删除商户
     * @param null $ids 商户id
     * @author PaulXun <paulxun@qq.com>
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($ids = null)
    {
        if ($ids === null) $this->error('请选择要操作的数据');
        $ids = is_array($ids) ? $ids : (array)$ids;
		
        // 获取商户标题和id
        $data_list = MerchModel::where('id', 'in', $ids)->column('short_name', 'id');
        
        foreach ($data_list as $id => $title) {        
            // 删除商户
            if (false ===  MerchModel::where('id', $id)->delete()) {
                $this->error('删除商户：'. $title. ' 失败');
            }
			// 清除缓存
			cache('merch_info_'.$id, null);
			// 删除商户对应管理员
			UserModel::where('role', 4)->where('group', $id)->delete();
            // 记录行为
            action_log('merch_delete', 'mall_merch', $id, UID, $title);
        }
        $this->success('删除成功');
    }
}
